package com.ccl.iot.controler;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;

import com.ccl.iot.privates.BitmapBuilder;
import com.ccl.iot.device.IOTDevice;
import com.ccl.iot.IOTInterface;
import com.ccl.iot.views.MarqueeLabel;
import com.ccl.iot.R;

public abstract class MiniControlerView extends ControlerView {
    private boolean iLabelVisible = true;

    private Rect iRect = new Rect();
    private Rect iTempRect = new Rect();

    private Rect iLabelRect = new Rect();
    private MarqueeLabel iLabel = null;

    private Rect iItemRect = new Rect();

    public MiniControlerView(Context context, IOTDevice aDevice) {
        super(context, aDevice);

        iLabel = new MarqueeLabel(this.getContext());

        this.addView(iLabel);
    }

    public Rect getItemRect() {
        return iItemRect;
    }

    private void updateItemRect() {
        if (iRect != null && iRect.width() > 0 && iRect.height() > 0) {
            int tWidthPadding, tHeightPadding;

            tWidthPadding = iRect.width() >> 4;
            tHeightPadding = iRect.height() >> 4;

            if (iLabelVisible) {
                iLabelRect.set(tWidthPadding, iRect.height() - iLabel.getFontHeight(), iRect.width() - tWidthPadding, iRect.height());

                iItemRect.set(tWidthPadding, tHeightPadding, iRect.width() - tWidthPadding, iRect.height() - iLabelRect.height() - tHeightPadding);
            } else {
                iItemRect.set(tWidthPadding, tHeightPadding, iRect.width() - tWidthPadding, iRect.height() - tHeightPadding);
            }
        } else {
            iItemRect.set(iRect);
        }

        this.postInvalidate();
    }

    public void setLabelVisibility(boolean aVisible) {
        if (iLabelVisible != aVisible) {
            iLabelVisible = aVisible;

            if (iLabel != null) {
                iLabel.setVisibility(aVisible ? VISIBLE : GONE);
            }

            updateItemRect();
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        iTempRect.set(l, t, r, b);

        if (!iTempRect.equals(iRect)) {
            iRect.set(iTempRect);
            updateItemRect();

            if (iLabelVisible) {
                iLabel.layout(iLabelRect.left, iLabelRect.top, iLabelRect.right, iLabelRect.bottom);
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (iRect.width() > 0 && iRect.height() > 0) {
            if (iLabelVisible && getItem() != null) {
                String tLabel = getItem().getName();
                iLabel.setText(tLabel);
            }
        }
    }


    @Override
    protected Rect calcRect() {
        Rect tRect = BitmapBuilder.GetBitmapRect(this.getContext(), R.drawable.icon_unknown);

        if (iLabelVisible) {
            tRect.bottom += iLabelRect.height();
        }

        return tRect;
    }

    public void onStateChanged(IOTInterface.IOTStateMonitorInterface aSource, int aNewState) {
        if (waitView != null && waitView.getVisibility() != GONE) {
            post(new Runnable() {
                @Override
                public void run() {
                    waitView.setVisibility(GONE);
                }
            });
        }
    }

}
